package com.liu.utils;

import com.liu.domain.student;
import com.liu.model.JsonResult;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

/**
 * Created by liu on 17-12-21.
 */
public class PoiWriteExcel {
    public static File writer(JsonResult result) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

        // 创建工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建sheet
        HSSFSheet sheet = workbook.createSheet();

        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = null;

        cell = row.createCell(0);
        cell.setCellValue("stu_id");
        cell = row.createCell(1);
        cell.setCellValue("room_id");
        cell = row.createCell(2);
        cell.setCellValue("name");
        cell = row.createCell(3);
        cell.setCellValue("sex");
        cell = row.createCell(4);
        cell.setCellValue("cla");
        cell = row.createCell(5);
        cell.setCellValue("password");
        cell = row.createCell(6);
        cell.setCellValue("acad");
        cell = row.createCell(7);
        cell.setCellValue("pro");
        cell = row.createCell(8);
        cell.setCellValue("time");
        cell = row.createCell(9);
        cell.setCellValue("birthday");
        cell = row.createCell(10);
        cell.setCellValue("phone");

        int j = 1;
        for (student stu : result.getData()) {
            // 创建行
            row = sheet.createRow(j);
                cell = row.createCell(0);
                cell.setCellValue(stu.getStu_id());
                cell = row.createCell(1);
                cell.setCellValue(stu.getRoom_id());
                cell = row.createCell(2);
                cell.setCellValue(stu.getName());
                cell = row.createCell(3);
                cell.setCellValue(stu.getSex());
                cell = row.createCell(4);
                cell.setCellValue(stu.getCla());
                cell = row.createCell(5);
                cell.setCellValue(stu.getPassword());
                cell = row.createCell(6);
                cell.setCellValue(stu.getAcad());
                cell = row.createCell(7);
                cell.setCellValue(stu.getPro());
                cell = row.createCell(8);
                cell.setCellValue(sdf.format(stu.getTime()));
                cell = row.createCell(9);
                cell.setCellValue(sdf.format(stu.getBirthday()));
                cell = row.createCell(10);
                cell.setCellValue(stu.getPhone());
                j++;
        }


        // excel文件
        File file = new File("excel.xls");
        if (!file.exists()) {
            file.createNewFile();
        }

        // 写入文件
        FileOutputStream out = new FileOutputStream(file);
        workbook.write(out);

        out.close();
        workbook.close();
        return file;
    }

    public static void main(String[] args) throws IOException {
        List<student> students = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            student student = new student();
            student.setPhone("fg");
            student.setBirthday(new Date());
            student.setTime(new Date());
            student.setPro("djflk");
            student.setCla("dfkl");
            student.setAcad("djflk");
            student.setPassword("djf");
            student.setName("jdfkl");
            student.setRoom_id(34);
            student.setStu_id("df");
            student.setSex("df");
            students.add(student);
        }
        JsonResult result = new JsonResult();
        result.setData(students);
        PoiWriteExcel.writer(result);
    }
}
